package com.jinrui.swing.widget;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import org.ym.util.ToolsUtil;

import com.jinrui.global.GlobalData;
import com.jinrui.model.LoginTask;
import com.jinrui.model.TaskChange;

public class TaskChangeTreePanel extends JPanel {
	JTree tree;
	JScrollPane scrollPane;
	JTreeModel jtreeModel;

	public TaskChangeTreePanel() {
		setLayout(new BorderLayout(0, 0));

		tree = new JTree();
		tree.setRootVisible(false);
		tree.setModel(jtreeModel = new JTreeModel());
		tree.setCellRenderer(new TreeCellRenderer());

		scrollPane = new JScrollPane(tree);
		add(scrollPane, BorderLayout.CENTER);
	}

	public void setTaskChanges(List<TaskChange> list) {
		jtreeModel.setTaskChanges(list);
	}

	public void setLoginTask(List<LoginTask> list) {
		jtreeModel.setLoginTask(list);
	}

	public void refresh() {
		tree.updateUI();
	}

	static class JTreeModel implements TreeModel {
		public static final String ROOT = "root";

		private List<TaskChange> taskChanges = new ArrayList();
		private List<LoginTask> loginTasks = new ArrayList();

		public void setTaskChanges(List<TaskChange> list) {
			taskChanges.clear();
			if (ToolsUtil.aCollection(list))
				taskChanges.addAll(list);
		}

		public void setLoginTask(List<LoginTask> list) {
			loginTasks.clear();
			if (ToolsUtil.aCollection(list))
				loginTasks.addAll(list);
		}

		@Override
		public Object getRoot() {
			return ROOT;
		}

		@Override
		public Object getChild(Object parent, int index) {
			if (parent instanceof String) {
				return taskChanges.get(index);
			} else if (parent instanceof TaskChange) {
				TaskChange taskc = (TaskChange)parent;
				List<LoginTask> taskChilds = getTaskChangeChild(taskc.getId());	
				return taskChilds.get(index);
			}
			return null;
		}

		@Override
		public int getChildCount(Object parent) {
			if (parent instanceof String) {
				return taskChanges.size();
			} else if (parent instanceof TaskChange) {
				TaskChange taskc = (TaskChange)parent;
				List<LoginTask> taskChilds = getTaskChangeChild(taskc.getId());	
				return taskChilds.size();
			}
			return 0;
		}

		@Override
		public boolean isLeaf(Object node) {
			return (node instanceof LoginTask);
		}

		@Override
		public void valueForPathChanged(TreePath path, Object newValue) {
			// TODO Auto-generated method stub

		}

		@Override
		public int getIndexOfChild(Object parent, Object child) {
			if(parent instanceof String)
			{
				TaskChange taskc = (TaskChange)child;
				int num = 0;
				for(TaskChange taskChange :taskChanges)
				{
					if(taskc.getId().equals(taskChange.getId()))
						return num;
					num++;
				}
			}else if(parent instanceof TaskChange)
			{
				TaskChange taskc = (TaskChange)parent;
				LoginTask loginTask = (LoginTask)child;
				List<LoginTask> taskChilds = getTaskChangeChild(taskc.getId());				
				int num = 0;
				for(LoginTask item:taskChilds)
				{
					if(loginTask.getId().equals(item.getId()))
						taskChilds.add(item);
					num++;
				}
				return num++;
			}
			return -1;
		}

		@Override
		public void addTreeModelListener(TreeModelListener l) {
			// TODO Auto-generated method stub

		}

		@Override
		public void removeTreeModelListener(TreeModelListener l) {
			// TODO Auto-generated method stub

		}
		
		private List<LoginTask> getTaskChangeChild(String taskChangeId)
		{
			List<LoginTask> taskChilds = new ArrayList();
			for(LoginTask item:loginTasks)
			{
				if(item.getTaskChangeId().equals(taskChangeId))
					taskChilds.add(item);
			}
			return taskChilds;
		}
	}

	public class TreeCellRenderer extends DefaultTreeCellRenderer {
		private  ImageIcon imageIcon;

		public Component getTreeCellRendererComponent(JTree tree, Object value,
				boolean selected, boolean expanded, boolean leaf, int row,
				boolean hasFocus) {
			super.getTreeCellRendererComponent(tree, value, selected, expanded,
					leaf, row, hasFocus);
			TreeItemPanel treeItem = new TreeItemPanel(selected,hasFocus);
			if (value instanceof TaskChange) {
				TaskChange entity =(TaskChange) value;
				treeItem.setText(entity.getName());
				String imagePath = GlobalData.getAssetPath() + "\\"
						+ ((TaskChange) value).getIconUri();
				treeItem.setIcon(imagePath);
				treeItem.setText(entity.getName());
			} else if (value instanceof LoginTask) {
				LoginTask entity =(LoginTask) value;
				treeItem.setText(entity.getName());
				String imagePath = GlobalData.getAssetPath() + "\\"
						+ entity.getIconUri();
				treeItem.setIcon(imagePath);
				treeItem.setText(entity.getName());
			} else {
				this.setText(value.toString());
				return this;
			}
			return treeItem;
		}
	}

}
